package com.hq.fxh.No2_BBS.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.No1_Infromation.Infor.Activity.NewsDetailsActivity;
import com.hq.fxh.No2_BBS.Adapter.Ladapter_KanTieList;
import com.hq.fxh.Modle.MyString;
import com.hq.fxh.Modle.KanTieListModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.CustomerView.PopupWindow_PingBi;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :论坛看帖内容页
 * @Created 2017/8/17
 * @Email xlwenhao1858@sina.com
 */


public class BBS_KanTie_ContentFragment extends Fragment {
    protected View rootView;
    protected PullToRefreshListView listViewKanTieContent;
    Ladapter_KanTieList adapter;
    List<KanTieListModle.DataBean> data;
    int pageNum = 1;

    int type; // 1 最新 2 热帖 3 最新回复
    String url = "http://10.10.15.136/fxhNew/ajax/new/postinfo.html";


    public void fetchData() {
        UserServiceImpl.GetKanTieListUrl(type, pageNum, 20, new Callback<KanTieListModle>() {
            @Override
            public void onResponse(Call<KanTieListModle> call, Response<KanTieListModle> response) {
                listViewKanTieContent.onRefreshComplete();
                KanTieListModle body = response.body();
                List<KanTieListModle.DataBean> data = body.getData();
                if (data != null&&data.size()!=0) {
                    if (BBS_KanTie_ContentFragment.this.data == null) {
                        BBS_KanTie_ContentFragment.this.data = new ArrayList<KanTieListModle.DataBean>();
                    } else {
                        if (pageNum == 1) {
                            BBS_KanTie_ContentFragment.this.data.clear();
                        }
                    }
                    BBS_KanTie_ContentFragment.this.data.addAll(data);
                    adapter.setData(BBS_KanTie_ContentFragment.this.data);
                    adapter.notifyDataSetChanged();

                } else {
                    LogUtils.startToash(getContext(), MyString.nullDataString);
                }

            }

            @Override
            public void onFailure(Call<KanTieListModle> call, Throwable t) {
                listViewKanTieContent.onRefreshComplete();

            }
        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_kantie_content, null);
        this.type = getArguments().getInt("type");
        initView(rootView);
        initData();
        fetchData();
        return rootView;
    }

    private void initData() {
        listViewKanTieContent.setMode(PullToRefreshBase.Mode.BOTH);
        listViewKanTieContent.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = 1;
                fetchData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = pageNum + 1;
                fetchData();

            }
        });
        adapter = new Ladapter_KanTieList(getContext(), new Ladapter_KanTieList.KanTieCallBack() {
            @Override
            public void onCall(int position, LinearLayout layout) {
                showPopup(layout, position);
            }

            @Override
            public void contentCall(int position, Object data) {
                Intent intent = new Intent(getContext(), NewsDetailsActivity.class);
                intent.putExtra("url",url);
                startActivity(intent);


            }
        });
        listViewKanTieContent.setAdapter(adapter);
    }

    PopupWindow_PingBi popupWindow_pingBi;

    private void showPopup(View view, final int parentsPosition) {
        View popview = View.inflate(getActivity(), R.layout.popup_bbs_pingbi, null);

        ListView list = (ListView) popview.findViewById(R.id.list_PopupBBS);

        List<String> objects = new ArrayList<>();

        objects.add("关注此人");
        objects.add("举报");
        objects.add("加入黑名单");
        objects.add("删除");
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                 clickPopup(parentsPosition, i);
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.pop_text_item, R.id.popup_Text_item, objects);
        list.setAdapter(adapter);
        Tool.setListViewHeightBasedOnChildren(list);
        popupWindow_pingBi = new PopupWindow_PingBi();
        popupWindow_pingBi.showPopupWindow(getActivity(), popview, view);

    }

    private void clickPopup(int parentsPosition, int i) {
        popupWindow_pingBi.dismissPopup();
        /*
        * 处理看帖中的弹窗点击事件
        * */

    }

    private void initView(View rootView) {
        listViewKanTieContent = (PullToRefreshListView) rootView.findViewById(R.id.listView_KanTie_Content);
    }

    public static BBS_KanTie_ContentFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        BBS_KanTie_ContentFragment fragment = new BBS_KanTie_ContentFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
